/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.sample.library.portlet.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.sample.library.model.Book;
import com.sample.library.portlet.helper.ASComparator;

/**
 * @author Brian Wing Shun Chan
 */
public class BookSearch extends SearchContainer<Book> {

	static List<String> headerNames = new ArrayList<String>();
	static Map<String, String> orderableHeaders = new HashMap<String, String>();

	static {
		headerNames.add("Book.Title");
		headerNames.add("Book.UserName");
		orderableHeaders.put("title", "Book.Title");
	}

	public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";

	public BookSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
		this(portletRequest, DEFAULT_CUR_PARAM, iteratorURL);
	}

	public BookSearch(
		PortletRequest portletRequest, String curParam,
		PortletURL iteratorURL) {

		super(
			portletRequest, new BookDisplayTerms(portletRequest),
			new BookSearchTerms(portletRequest), curParam, DEFAULT_DELTA,
			iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);

		BookDisplayTerms displayTerms = (BookDisplayTerms)getDisplayTerms();
//		BookSearchTerms searchTerms = (BookSearchTerms)getSearchTerms();

//		PortletConfig portletConfig =
//				(PortletConfig)portletRequest.getAttribute(
//					JavaConstants.JAVAX_PORTLET_CONFIG);
//
//		String portletName = portletConfig.getPortletName();
//
//		if (!portletName.equals(PortletKeys.USERS_ADMIN)) {
//			displayTerms.setStatus(WorkflowConstants.STATUS_APPROVED);
//			searchTerms.setStatus(WorkflowConstants.STATUS_APPROVED);
//		}
		
		iteratorURL.setParameter(
			BookDisplayTerms.TITLE, String.valueOf(displayTerms.getTitle()));
		iteratorURL.setParameter(
				BookDisplayTerms.USER_ID, String.valueOf(displayTerms.getUserId()));

		try {
//			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(portletRequest);
//
			String orderByCol = ParamUtil.getString(portletRequest, "orderByCol");
			String orderByType = ParamUtil.getString(portletRequest, "orderByType");
//
//			if (Validator.isNotNull(orderByCol) &&	Validator.isNotNull(orderByType)) {
//				preferences.setValue(
//					ASPortletKeys.LIBRARY_PORTLET_NAME, "users-order-by-col", orderByCol);
//				preferences.setValue(
//					ASPortletKeys.LIBRARY_PORTLET_NAME, "users-order-by-type",
//					orderByType);
//			}
//			else {
//				orderByCol = preferences.getValue(
//					ASPortletKeys.LIBRARY_PORTLET_NAME, "users-order-by-col", "Book.Title");
//				orderByType = preferences.getValue(
//					ASPortletKeys.LIBRARY_PORTLET_NAME, "users-order-by-type", "asc");
//			}

			OrderByComparator orderByComparator =getBookOrderByComparator(orderByCol, orderByType);

			setOrderableHeaders(orderableHeaders);
			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		}
		catch (Exception e) {
			_log.error(e);
		}
	}

	private OrderByComparator getBookOrderByComparator(String orderByCol, String orderByType) {

		OrderByComparator orderByComparator = null;
		if (orderByCol.equals("Book.Title")) {
			orderByComparator = new ASComparator("title");
		}
		else {
			orderByComparator = new ASComparator("title");
		}

		return orderByComparator;
	}
	private static Log _log = LogFactoryUtil.getLog(BookSearch.class);

}